package com.pmsystem.service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.pmsystem.dao.AdministratorMapper;
import com.pmsystem.dao.UserMapper;
import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.AdministratorExample;
import com.pmsystem.pojo.AdministratorExample.Criteria;
import com.pmsystem.pojo.Remove;
import com.pmsystem.pojo.User;
import com.pmsystem.pojo.UserExample;
import com.pmsystem.util.BeanUtil;
import com.pmsystem.util.MD5;


@Service
public class AdministratorService {

	
	@Autowired
	private AdministratorMapper administratorMapper;
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 
	 * <p>Title: findAdmin</p >  
	 * <p>Description: 按一定条件查找Admin信息，注意SQL</p >  
	 * @author 泽强
	 * @param map 
	 * @return
	 */
	public List<Administrator> findAdmin(Map<String, Object> map) {
		Integer pageNo = (Integer) map.get("pageNo");
		Integer pageSize = (Integer) map.get("pageSize");
		if(pageNo!=null&&pageSize!=null) {
			int start = (pageNo - 1) * pageSize;
			map.put("start", start);
		} 
		return administratorMapper.findAdmin(BeanUtil.mapToMapWithoutNull(map));
	}

	
	/**
	 * 
	 * <p>Title: addAdministrator</p>
	 * <p>Description:  增加二级管理员</p> 
	 * @author 泽强
	 * @param administrator
	 * @return
	 */
	
	public int addAdministrator(Administrator administrator) {
		String encodePwd = MD5.MD5Eencryption(administrator.getAdminPassword(), administrator.getAdminAcc());
		if(encodePwd==null)
			return 0;
		administrator.setAdminPassword(encodePwd);
		return administratorMapper.insertSelective(administrator);
	}

	/**
	 * 
	 * <p>Title: deleteAdministrator</p>
	 * <p>Description: 根据id批量删除二级管理员 </p> 
	 * @author 泽强
	 * @param idList
	 */



	/**
	 * 
	 * <p>Title: updateAdministrator</p>
	 * <p>Description: 更新二级管理员信息 </p> 
	 * @author 泽强
	 * @param administrator
	 */
	public boolean updateAdministrator(Administrator administrator) {
		
		int flag = administratorMapper.updateByPrimaryKeySelective(administrator);
		return flag==1?true:false;
	}

	/**
	 * 
	 * <p>Title: login</p >  
	 * <p>Description: 二级管理员登陆</p >  
	 * @author 泽强
	 * @param username
	 * @param password
	 * @return
	 */
	public Administrator login(String username, String password) {
		String encodePwd = MD5.MD5Eencryption(password, username);
		if(encodePwd==null)
			return null;
		return administratorMapper.login(username,encodePwd);
	}


	/**  
	 * <p>Title: findUserUncheck</p >  
	 * <p>Description:查询所有待审核用户信息</p >  
	 * @author 魏家洛
	 * @return  
	 */  
	public List<User> findUserUncheck() {
		return userMapper.selectUserEableEq2();
	}


	/**  
	 * <p>Title: passUser</p >  
	 * <p>Description:通过业主注册账号的审核</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return  
	 */  
	public boolean passUser(String userId) {
		return userMapper.updateUserBeUse(userId)==1;
	}


	/**  
	 * <p>Title: deleteUser</p >  
	 * <p>Description:删除用户或不通过业主账号注册申请</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return  
	 */  
	/**
	 * 
	 * <p>Title: deleteUser</p>  
	 * <p>Description:重写方法:删除用户或不通过业主账号注册申请 </p>  
	 * @author 树昌
	 * @param userId
	 * @return
	 */
//	public boolean deleteUser(String userId) {
//		return userMapper.deleteUser(userId)==1;
//	}
	public boolean deleteUser(int id) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdEqualTo(id);
		User user = new User();
		user.setEnable((byte) 0);
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: deleteAllUser</p>  
	 * <p>Description:批量删除业主用户 </p>  
	 * @author 树昌
	 * @param listID
	 * @return
	 */
	public boolean deleteAllUser(List<Integer> listID) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdIn(listID);
		User user = new User();
		user.setEnable((byte) 0);
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
	
	/**
	 * 
	 * <p>Title: deleteAllUser</p>  
	 * <p>Description:批量通过业主用户 </p>  
	 * @author 树昌
	 * @param listID
	 * @return
	 */
	public boolean checkedAllUser(List<Integer> listID) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdIn(listID);
		User user = new User();
		user.setEnable((byte) 1);
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: findUser</p>  
	 * <p>Description:查询所有已注册的合法用户 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<User> findUser(String keyword){
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria =userExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);

		if(keyword!=null) {
			keyword = "%" + keyword + "%";
			criteria.andUserNameLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria2 = userExample.or();
			criteria2.andPersonSexLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria3 = userExample.or();
			criteria3.andPresonNameLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria4 = userExample.or();
			criteria4.andTelephoneLike(keyword);
		}
		
		List<User> listuser = userMapper.selectByExample(userExample);
		
		//安全处理，将密码信息全部移除
		for (User user : listuser) {
			user.setUserPassword(null);
		}
		
		return listuser;
		
	}
	
	
	/**
	 * 
	 * <p>Title: findUserByUserId</p>  
	 * <p>Description:根据业主ID查询业主账号信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public User findUserByUserId(int id) {
		User user = userMapper.selectByPrimaryKey(id);
		
		//安全处理
		user.setUserPassword(null);
		return user;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateUser</p>  
	 * <p>Description:修改业主信息 </p>  
	 * @author 树昌
	 * @param user
	 * @return
	 */
	public boolean updateUser(User user) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdEqualTo(user.getUserId());
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: updateUserPassword</p>  
	 * <p>Description:管理员修改业主密码 </p>  
	 * @author 树昌
	 * @param user
	 * @return
	 */
	public boolean updateUserPassword(User user) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdEqualTo(user.getUserId());
		
		User userData = userMapper.selectByPrimaryKey(user.getUserId());
		String userName = userData.getUserName();
		String password = user.getUserPassword();
		String encodePwd = MD5.MD5Eencryption(password, userName);
		user.setUserPassword(encodePwd);
		
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: showAllAdmin</p>  
	 * <p>Description:查询所有的管理员账户 </p>  
	 * @author 树昌
	 * @param keyword
	 * @return
	 */
	public List<Administrator> showAllAdmin(String keyword){
		
		AdministratorExample administratorExample = new AdministratorExample();
		Criteria criteria =administratorExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		
		if(keyword!=null) {
			keyword="%"+keyword+"%";
			criteria.andAdminAccLike(keyword);
			Criteria criteria2 = administratorExample.or();
			criteria2.andAuthorityLike(keyword);
			Criteria criteria3 = administratorExample.or();
			criteria3.andResponsibleLike(keyword);
			Criteria criteria4 = administratorExample.or();
			criteria4.andTelephoneLike(keyword);
		}
		
		return administratorMapper.selectByExample(administratorExample);
		
	}
	
	
	/**
	 * 
	 * <p>Title: deleteOneAdmin</p>  
	 * <p>Description:删除一个管理员账号 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public boolean deleteOneAdmin(int id) {
		
		AdministratorExample administratorExample = new AdministratorExample();
		Criteria criteria = administratorExample.createCriteria();
		criteria.andAdminIdEqualTo(id);
		Administrator administrator = new Administrator();
		administrator.setEnable((byte) 0);
		int row = administratorMapper.updateByExampleSelective(administrator, administratorExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllAdmin</p>  
	 * <p>Description:批量删除管理员账号 </p>  
	 * @author 树昌
	 * @param listID
	 * @return
	 */
	public boolean deleteAllAdmin(List<Integer> listID) {
		
		AdministratorExample administratorExample = new AdministratorExample();
		Criteria criteria = administratorExample.createCriteria();
		criteria.andAdminIdIn(listID);
		Administrator administrator = new Administrator();
		administrator.setEnable((byte) 0);
		int row = administratorMapper.updateByExample(administrator, administratorExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: addAdminacc</p>  
	 * <p>Description:添加一个管理员账号 </p>  
	 * @author 树昌
	 * @param administrator
	 * @return
	 */
	public boolean addAdminacc(Administrator administrator) {
		
		String encodePwd = MD5.MD5Eencryption(administrator.getAdminPassword(), administrator.getAdminAcc());
		administrator.setAdminPassword(encodePwd);
		int row = administratorMapper.insertSelective(administrator);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: findAdminByID</p>  
	 * <p>Description:根据ID查询管理员账号信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public Administrator findAdminByID(int id) {
		
		Administrator administrator = administratorMapper.selectByPrimaryKey(id);
		//安全处理
		administrator.setAdminPassword(null);
		return administrator;
	}
	
	
	/**
	 * 
	 * <p>Title: updateAdminPassword</p>  
	 * <p>Description:修改管理员的密码 </p>  
	 * @author 树昌
	 * @param admin
	 * @return
	 */
	public boolean updateAdminPassword(Administrator admin) {
		
		AdministratorExample administratorExample = new AdministratorExample();
		Criteria criteria = administratorExample.createCriteria();
		criteria.andAdminIdEqualTo(admin.getAdminId());
		
		Administrator adminData = administratorMapper.selectByPrimaryKey(admin.getAdminId());
		String adminAcc = adminData.getAdminAcc();
		String password = admin.getAdminPassword();
		String encodePwd = MD5.MD5Eencryption(password, adminAcc);
		admin.setAdminPassword(encodePwd);
		
		int row = administratorMapper.updateByExampleSelective(admin, administratorExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showRemove</p>  
	 * <p>Description:查询所有被逻辑删除的账号 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<Remove> showRemove(String keyword) {
		
		List<Remove> listRemove = new ArrayList<>();
		//查询所有被逻辑删除的管理员账号
		AdministratorExample administratorExample = new AdministratorExample();
		Criteria ADcriteria = administratorExample.createCriteria();
		ADcriteria.andEnableEqualTo((byte) 0);
		
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			ADcriteria.andAdminAccLike(keyword);
			Criteria ADcriteria2 = administratorExample.or();
			ADcriteria2.andResponsibleLike(keyword);
		}
		
		List<Administrator> listAdmin = administratorMapper.selectByExample(administratorExample);
		if(listAdmin!=null) {
			for (Administrator admin : listAdmin) {
				Remove remove = new Remove(admin.getAdminId(), admin.getAdminAcc(), admin.getResponsible(), 1);
				listRemove.add(remove);
			}
		}
		
		//查询所有被逻辑删除的用户账号
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria Uscriteria = userExample.createCriteria();
		Uscriteria.andEnableEqualTo((byte) 0);
		
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			Uscriteria.andUserNameLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria Uscriteria2 = userExample.or();
			Uscriteria2.andPresonNameLike(keyword);
		}
		
		List<User> listUser = userMapper.selectByExample(userExample);
		if(listUser!=null) {
			for (User user : listUser) {
				Remove remove = new Remove(user.getUserId(), user.getUserName(), user.getPresonName(), 2);
				listRemove.add(remove);
			}
		}
		
		return listRemove;
	}
	
	/**
	 * 
	 * <p>Title: recoveryAcc</p>  
	 * <p>Description:恢复账号的使用 </p>  
	 * @author 树昌
	 * @param AccID
	 * @param isAdmin
	 * @return
	 */
	public boolean recoveryAcc(int AccID,int isAdmin) {
		
		int row = 0;
		if(isAdmin==1) {
			AdministratorExample administratorExample = new AdministratorExample();
			Criteria ADcriteria = administratorExample.createCriteria();
			ADcriteria.andAdminIdEqualTo(AccID);
			Administrator admin = new Administrator();
			admin.setEnable((byte) 1);
			row =administratorMapper.updateByExampleSelective(admin, administratorExample);
		}
		
		if(isAdmin==2) {
			UserExample userExample = new UserExample();
			com.pmsystem.pojo.UserExample.Criteria UScriteria = userExample.createCriteria();
			UScriteria.andUserIdEqualTo(AccID);
			User user = new User();
			user.setEnable((byte) 1);
			row = userMapper.updateByExampleSelective(user, userExample);
		}
		
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: permanentDelete</p>  
	 * <p>Description:永久性删除账号 </p>  
	 * @author 树昌
	 * @param AccID
	 * @param isAdmin
	 * @return
	 */
	public boolean permanentDelete(int AccID,int isAdmin) {
		
		int row = 0;
		if(isAdmin==1) {
			AdministratorExample administratorExample = new AdministratorExample();
			Criteria ADcriteria = administratorExample.createCriteria();
			ADcriteria.andAdminIdEqualTo(AccID);
			row =administratorMapper.deleteByExample(administratorExample);
		}
		
		if(isAdmin==2) {
			UserExample userExample = new UserExample();
			com.pmsystem.pojo.UserExample.Criteria UScriteria = userExample.createCriteria();
			UScriteria.andUserIdEqualTo(AccID);
			row = userMapper.deleteByExample(userExample);
		}
		
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showRegist</p>  
	 * <p>Description:查询所有提交注册申请的用户 </p>  
	 * @author 树昌
	 * @param keyword
	 * @return
	 */
	public List<User> showRegist(String keyword){
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andEnableEqualTo((byte) 2);
		
		if(keyword!=null) {
			keyword="%"+keyword+"%";
			criteria.andUserNameLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria2 = userExample.or();
			criteria2.andPresonNameLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria3 = userExample.or();
			criteria3.andPersonSexLike(keyword);
			com.pmsystem.pojo.UserExample.Criteria criteria4 = userExample.or();
			criteria4.andTelephoneLike(keyword);
		}
		
		List<User> listUser = userMapper.selectByExample(userExample);
		//安全处理
		for (User user : listUser) {
			user.setUserPassword(null);
		}
		return listUser;
	}
	
	
	/**
	 * 
	 * <p>Title: userChecked</p>  
	 * <p>Description:根据ID将用户账户的状态设为可用,已通过 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public boolean userChecked(int id) {
		
		UserExample userExample = new UserExample();
		com.pmsystem.pojo.UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserIdEqualTo(id);
		User user = new User();
		user.setEnable((byte) 1);
		int row = userMapper.updateByExampleSelective(user, userExample);
		return row==1?true:false;
	}
}
